package com.lfy.modules.safety.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lfy.modules.common.domain.BaseEnum;
import com.lfy.modules.common.domain.PageResultVO;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.utils.RequestUtil;
import com.lfy.modules.safety.blankMenu.BlackMenuCache;
import com.lfy.modules.safety.domain.BlackList;
import com.lfy.modules.common.utils.PageUtil;
import com.lfy.modules.safety.domain.dto.BlackDTO;
import com.lfy.modules.safety.domain.vo.BlackListVO;
import com.lfy.modules.safety.mapper.BlackListMapper;
import com.lfy.modules.safety.service.BlackListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 吕飞扬
* @description 针对表【black_list】的数据库操作Service实现
* @createDate 2024-05-01 00:50:41
*/
@Service
public class BlackListServiceImpl
    implements BlackListService{
    @Resource
    private BlackListMapper blackListMapper;
    @Override
    public R<PageResultVO<BlackListVO>> getBlackListPage(Integer pageNum, Integer pageSize, String keyword) {
        Page<BlackList> blackListPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BlackList> blackListLambdaQueryWrapper = new LambdaQueryWrapper<BlackList>();
        if(StringUtils.isNotBlank(keyword)){
            blackListLambdaQueryWrapper.like(BlackList::getIpAddress, keyword).or().like(BlackList::getIp, keyword).or().like(BlackList::getRemarks, keyword);
        }
        blackListLambdaQueryWrapper.orderByDesc(BlackList::getCreateTime);
        IPage<BlackList> result = this.blackListMapper.selectPage(blackListPage, blackListLambdaQueryWrapper);
        List<BlackList> records = result.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return R.success(PageUtil.convert2PageEmptyResult(result, BlackListVO.class));
        }
        List<BlackListVO> listVOS = records.stream().map(item -> {
            BlackListVO blackListVO = new BlackListVO();
            BeanUtils.copyProperties(item, blackListVO);
            if(item.getBecause()!= null){
                blackListVO.setBecause(BaseEnum.BlackEnum.getMsg(item.getBecause()));
            }
            if(item.getCreateTime() != null){
                blackListVO.setCreateTime(DateUtil.formatDateTime(item.getCreateTime()));
            }
            return blackListVO;
        }).sorted(Comparator.comparing(BlackListVO::getCreateTime,Comparator.nullsLast(Comparator.reverseOrder()))).collect(Collectors.toList());
        return R.success(PageUtil.buildPageResultVO(result,listVOS));
    }

    @Override
    public R<Void> addBlack(BlackDTO dto) {
        BlackList blackList = new BlackList();
        BeanUtils.copyProperties(dto, blackList);
        blackList.setBecause(BaseEnum.BlackEnum.BLACK_MANUAL.getCode());
        blackList.setIpAddress(RequestUtil.getPlace(dto.getIp()));
        this.blackListMapper.insert(blackList);
        BlackMenuCache.addBlackMenu(dto.getIp());
        return R.success();
    }

    @Override
    public R<Void> removeBlack(BlackDTO dto) {
        this.blackListMapper.deleteById(dto.getId());
        BlackMenuCache.removeBlackMenuByKey(dto.getIp());
        return R.success();
    }


}




